﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Red.Core;

namespace Red.Controls.ViewModels
{
    public class RedMenuDataItem : ViewModel
    {
        private bool _isEnabled;

        /// <summary>
        ///     Underlaying command wrapper 
        /// </summary>
        public CommandWrapper Wrapper { get; private set; }

        /// <summary>
        ///     Hierarchical children of this item
        /// </summary>
        public IEnumerable< RedMenuDataItem > Children { get; private set; }

        /// <summary>
        ///     Helper property ( comes in handy when Children.Count is not enough )
        /// </summary>
        public bool HasItems { get { return Children?.Any() ?? false; } }

        /// <summary>
        ///     Items label
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        ///     If specified it replaces RedIcon with a RedThumbnail
        /// </summary>
        public ImageSource ImageSource { get; private set; }

        /// <summary>
        ///     Binds visual property of this item, 'true' by default
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                if ( value != _isEnabled )
                {
                    _isEnabled = value;
                    OnPropertyChanged();
                }
            }
        }

        public RedMenuDataItem( CommandWrapper wrapper, string name = null, IEnumerable<RedMenuDataItem> children = null, ImageSource imageSource = null )
        {
            // if any of the following contains a name then don't look deeper: RedMenuDataItem > CommandWrapper > RedCommand
            Name = !string.IsNullOrEmpty( name ) ? name : wrapper?.Name;
            Children = children ?? new List<RedMenuDataItem>();
            Wrapper = wrapper;
            ImageSource = imageSource;
            IsEnabled = true;
        }
    }
}
